package com.example.demo001.common.result;

import com.example.demo001.constant.StatusCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 后端统一返回结果
 *
 * @param <T>
 */
@Data
public class Result<T> implements Serializable {
    //编码：1成功，0和其它数字为失败
    private Integer code;
    //错误信息
    private String msg;
    //数据
    private T data;

    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Result() {

    }

    public static <T> Result<T> success() {
        Result<T> result = new Result<>();
        result.code = StatusCode.OK;
        return result;
    }
    public static <T> Result<T> success(String msg, T object) {
        Result<T> result = new Result<>();
        result.code = StatusCode.OK;
        result.msg = msg;
        result.data = object;
        return result;
    }



    public static <T> Result<T> success(T object) {
        Result<T> result = new Result<>();
        result.data = object;
        result.code = StatusCode.OK;
        return result;
    }


    public static <T> Result<T> error(String msg) {
        Result<T> result = new Result<>();
        result.msg = msg;
        result.code = StatusCode.ERROR;
        return result;
    }

    public static <T> Result<T> error(Integer code, String msg) {
        Result<T> result = new Result<>();
        result.msg = msg;
        result.code = code;
        return result;
    }
    public static <T> Result<T> error(Integer code, String msg , T data) {
        Result<T> result = new Result<>();
        result.msg = msg;
        result.code = code;
        result.data = data;
        return result;
    }


}
